package com.ruoyi.framework.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.HttpUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.manager.factory.AsyncFactory;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.SysReceive;
import com.ruoyi.project.system.mapper.SysReceiveMapper;
import com.ruoyi.project.tool.swagger.UserController.SignUtil;
import okhttp3.HttpUrl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.UUID;

/**
 * @description:
 * @author: gkk
 * @date: Created in 2021/8/26 13:37
 */
@Component
public class AutoUpdateDateSchedule {


    @Autowired
    private SysReceiveMapper sysReceiveMapper;


    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private WxMaService wxMaService;


/*    //每一分钟执行一次
    @Scheduled(cron = "0 0/1 * * * ?")
    public void dealUpdateTask() {
        SysReceive query = new SysReceive();
        query.setStatus("0");
        List<SysReceive> list = sysReceiveMapper.selectSysReceiveList(query);
        if (!list.isEmpty()) {
            for (SysReceive sysReceive : list) {
                if (StringUtils.isNotEmpty(sysReceive.getReason())) {
                    System.out.println("定时任务补发立减金:"+sysReceive.getOpenId());
                    String url = "https://api.mch.weixin.qq.com/v3/marketing/favor/users/" + sysReceive.getOpenId() + "/coupons";
                    JSONObject data = new JSONObject();
                    data.put("out_request_no", sysReceive.getOrders());//订单号
                    data.put("stock_id", sysReceive.getType());//代金券批次id也就是商品编号
                    data.put("appid", wxMaService.getWxMaConfig().getAppid());
                    data.put("stock_creator_mchid", "1605617216");//商户号
                    SignUtil signUtil = new SignUtil();
                    JSONObject result = null;
                    try {
                        result = new JSONObject();
//                       result.put("coupon_id", UUID.randomUUID());
                        result = HttpUtil.doPost(url, data, signUtil.getToken("POST", HttpUrl.parse(url), data.toJSONString()));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (result.containsKey("coupon_id")) {
                        sysReceive.setStatus("1");
                        sysReceive.setRemark(result.getString("coupon_id"));
                        sysReceive.setUsed("SENDED");
                        sysReceive.setUpdateTime(DateUtils.getNowDate());
                        int flag = sysReceiveMapper.updateSysReceive(sysReceive);
                        if (flag == 1) {
                            JSONObject jsonObject = new JSONObject();
                            jsonObject.put("dictCode", sysReceive.getType());
                            jsonObject.put("openId", sysReceive.getOpenId());
                            jsonObject.put("phone", sysReceive.getPhone());
                            jsonObject.put("couponId", sysReceive.getRemark());
                            AsyncManager.me().execute(AsyncFactory.sendPayment(rabbitTemplate, jsonObject));
                        }
                    }
                }
            }

        }
    }*/
}
